import 'dart:convert'; // 用于JSON解码
import 'package:flutter/material.dart';
import 'package:up_demo/storage_util.dart'; // 引入您的 StorageUtil

class CacheList extends StatefulWidget {
  const CacheList({super.key});

  @override
  State<CacheList> createState() => _CacheListState();
}

class _CacheListState extends State<CacheList> {
  // 定义用于本地存储的键，与之前上传页面保持一致
  static const String _keyCachedInspections = 'cached_inspections';

  List<Map<String, dynamic>> _cachedInspections = []; // 用于存储解析后的缓存数据

  @override
  void initState() {
    super.initState();
    _loadCachedData(); // 页面初始化时加载缓存数据
  }

  // 从本地加载所有缓存的巡检数据
  Future<void> _loadCachedData() async {
    String? cachedString = StorageUtil.getString(_keyCachedInspections);
    if (cachedString != null && cachedString.isNotEmpty) {
      try {
        List<dynamic> decodedList = jsonDecode(cachedString);
        setState(() {
          _cachedInspections = decodedList.cast<Map<String, dynamic>>();
        });
      } catch (e) {
        print("解析缓存数据失败: $e");
        setState(() {
          _cachedInspections = []; // 解析失败时清空列表
        });
      }
    } else {
      setState(() {
        _cachedInspections = []; // 如果没有缓存数据，也清空列表
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFf5f5f5),
      appBar: AppBar(
        title: const Text('本地缓存列表',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 18)),
        backgroundColor: const Color(0xFFffffff), // 与上传页面保持一致的 AppBar 颜色
        iconTheme: const IconThemeData(color: Colors.black), // 设置返回按钮颜色
      ),
      body: _cachedInspections.isEmpty
          ? const Center(
        child: Text(
          '暂无缓存数据',
          style: TextStyle(fontSize: 16, color: Colors.grey),
        ),
      )
          : ListView.builder(
        padding: const EdgeInsets.all(16.0),
        itemCount: _cachedInspections.length,
        itemBuilder: (context, index) {
          final inspection = _cachedInspections[index];
          return Card(
            margin: const EdgeInsets.only(bottom: 12.0),
            color: Colors.white,
            elevation: 2,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0),
            ),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '巡检人: ${inspection['inspectorName'] ?? '未知'}',
                    style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                  const SizedBox(height: 8),
                  Text('巡检位置: ${inspection['inspectionLocation'] ?? '未知'}'),
                  const SizedBox(height: 4),
                  Text('巡检设备: ${inspection['inspectionDevice'] ?? '未知'}'),
                  const SizedBox(height: 4),
                  Text('巡检内容: ${inspection['inspectionContent'] ?? '未知'}'),
                  const SizedBox(height: 4),
                  Text('巡检状态: ${inspection['status'] == 1 ? '有问题' : '没有问题'}'),
                  const SizedBox(height: 4),
                  Text('开始时间: ${inspection['startDate'] ?? '未知'}'),
                  const SizedBox(height: 4),
                  Text('结束时间: ${inspection['endDate'] ?? '未知'}'),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}